const userService = require("../service/UserService")
const jwt = require("../utils/jwt")





const userController = {
    login: async (req, res) => {
        const { username, password, email } = req.body
        console.log(username, password, email);
        const user = await userService.login(username, password)

        if (user.length === 1) {
            const token = jwt.generate({ username: user[0].username, password: user[0].username }, "600s")
            res.header("Authorization", token)

            res.send({ status: 1, message: "登录成功" })
            console.log(user);
            return
        }
        res.send({ status: 0, message: "登录失败" })
    },
    reigister: async (req, res) => {
        const { username, password, email } = req.body
        const reslut = await userService.findUserNmae(username)
        if (reslut.length === 0) {
            const reg = await userService.register(username, password, email)
            res.send({ register: "注册成功" })
        } else {
            res.send({ error: "用户存在" })
        }

    },
    add: async (req, res) => {

        let image = req.body.image
        if (image === undefined) {
            image = "uploads/1741488685542-612632785.png"
        }
        console.log(image);

        const { username, password, email } = req.body
        try {

            const result = await userService.add(username, password, email, image)
            res.send({ status: 1, message: "添加成功" })
        } catch (error) {
            res.send({ status: 0, message: "添加失败" })
        }
    },
    update: async (req, res) => {
        const { username, password, email } = req.body
        try {
            const result = await userService.update(req.params.id, username, password, email)
            res.send({ status: 1, message: "更新成功" })
        } catch (error) {
            res.send({ status: 0, message: "更新失败" })
        }
    },
    delete: async (req, res) => {
        try {
            const result = await userService.delete(req.params.id)
            console.log(result);
            res.send({ status: 1, message: "删除成功" })
        } catch (error) {
            res.send({ status: 1, message: "删除失败" })
        }

        // try {
        //     const result = await userService.update(id, username, password, email)
        //     res.send({ status: 1, message: "更新成功" })
        // } catch (error) {
        //     res.send({ status: 0, message: "更新失败" })
        // }

    },
    users: async (req, res) => {
        try {
            const result = await userService.users()
            const list = result.map(({ _id, username, password, email, image }) => {
                return { _id, username, password, email, image }
            })

            res.status(200).send({ status: 1, message: list })
        } catch (error) {
            res.send({ status: 0, message: "查询失败" })
        }


    },
    loginOut: async (req, res) => {

        try {
            req.session.destroy(function (err) {
                // cannot access session here
                res.send({ status: 1, message: "退出成功" })
            })

        } catch (error) {
            res.send({ status: 0, message: "退出失败" })
        }

    },
    upload: async (req, res) => {
        res.send({ status: 1, image: "uploads/" + req.file.filename, message: "上传成功" })
    }






}

module.exports = userController